<?php

/**
 * UserIdentity represents the data needed to identity a user.
 * It contains the authentication method that checks if the provided
 * data can identity the user.
 */
class UserIdentity extends CUserIdentity
{
	private $_id;
	/**
	 * Authenticates a user using the User data model.
	 * @return boolean whether authentication succeeds.
	 */
	public function authenticate()
	{
		$user = User::model()->findByAttributes(array('username'=>$this->username));
		if($user===null)
		{
			$this->errorCode=self::ERROR_USERNAME_INVALID;
		}
		else
		{
			/*if($user->password!==$user->encrypt($this->password))*/
			if($user->password!==md5($this->password))
			{
				$this->errorCode=self::ERROR_PASSWORD_INVALID;
			}
			else
			{
				$this->_id = $user->id;
				/*if(null === $user->user_lastvisit)
				{
					$lastLogin = time();
				}
				else
				{
					$lastLogin = strtotime($user->user_lastvisit);
				}*/
				//$this->setState('lastLoginTime', $lastLogin); 
				//$this->setState('fullName', $user->getFullName());
				//$this->setState('companyId', $user->user_company);
				//$company = Company::model()->findByPk($user->user_company);  
				//$this->setState('companyName', $company->company_name);
				//$this->setState('role', $user->getRoleOfUser()); 
				//$this->setState('loginAs', $user->getRoleOfUser()); 
				//$this->setState('dateFormat', Yii::app()->params['dateFormat']);
				//$this->setState('pageSize', Yii::app()->params['pageSize']);
				$this->errorCode=self::ERROR_NONE;
			}
		}
		return !$this->errorCode;
	}
	public function getId()
	{
		return $this->_id;
	}
}